import json
from config import *
import os
from base_class.inspect_logging import print_to_log
import shutil
import gzip
import re
import io

def upmaterial(filePath, filename, process, TYPE):
    if 'Fridge' in TYPE:
        s3rootpath = s3Fridgerootpath
    elif 'AC' in TYPE:
        s3rootpath = s3ACrootpath
    else:
        return Exception("TYPE输入错误")
    match =re.search(r"(\d{4}-\d{1,2}-\d{1,2})", filename)
    if match:
        jsonname = 'processdir/'+match.group()+'.json'
        filepath = TYPE+jsonname
        exists = os.path.exists(filepath)
        if exists:
            outpathls, filenames = process.data_process(filePath, 'processdir/'+match.group())
        else:
            process.unzip(filePath, filename)
            outpathls, filenames = process.data_process(filePath, process.dirnames.pop())
    for i, fullname in enumerate(outpathls):
        with open(fullname, mode='rb') as f:
            line = f.readline()
            while line:
                if isinstance(line, bytes):
                    line = line.decode(encoding="utf8", errors="ignore")
                    try:
                        dc = json.loads(line.replace('},', '}'))
                        material = dc["sn"][3:10]
                        dc["material"] = material
                        materialpath = fullname.replace('.json', '/') + f"{material}/"
                        if not os.path.exists(materialpath):
                            os.makedirs(materialpath)
                        with open(materialpath + f'{material}.json', mode='a+') as write:
                            js_str = json.dumps(dc, ensure_ascii=False)
                            write.write(js_str + ',\n')
                    except:
                        pass
                line = f.readline()
    for root, dirs, files in os.walk(fullname.replace('.json', '/'), topdown=False):
        if not dirs:
            for file in files:
                filepath = root + "/" + file
                p_date = root.split("/")[-2]
                material = root.split("/")[-1]
                s3path = s3rootpath+f"p_date={p_date}/" + f"material={material}/" + file.replace('.json', '.gz')
                zippath = root+'/'+file.replace('.json', '.gz')
                zip(zippath, filepath)
                process.upload(bucket, s3path, zippath)
                print_to_log(p_date, material, "上传成功")
                shutil.rmtree(root)
    shutil.rmtree(fullname.replace('.json', '/'))
    os.remove(outpathls[0])
    print_to_log("清理完成")


def zip(output_file_name, filepath):
    with gzip.open(output_file_name, 'wb') as output, io.TextIOWrapper(output, encoding='utf-8') as encode, open(filepath, mode='rb') as f:
        line = f.readline()
        while line:
            line = line.decode(encoding="utf8", errors="ignore")
            try:
                dc = json.loads(line.replace('},', '}'))
                js_str = json.dumps(dc, ensure_ascii=False)
                encode.write(js_str)
                encode.write(',')
                encode.write('\n')
                line = f.readline()
            except:
                pass




